﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CreatePGSQLDataScript
{
    public partial class TaskView : Form
    {
        DataAccess.SqlProvider db;
        TaskList parentForm;
        Class.Task tk;
        public TaskView(TaskList _ParentForm)
        {
            parentForm = _ParentForm;

            if (parentForm.tk != null)
            {
                tk = parentForm.tk;
            }
            InitializeComponent();
        }

        private void bindColumn(string fieldname, ComboBox comb1)
        {
            string sqlText = "select distinct " + fieldname + " as value from tasklist";
            comb1.DisplayMember = "value";
            comb1.ValueMember = "value";
            DataTable dt = db.GetDataTable(sqlText);

            DataRow dr = dt.NewRow();
            dr[0] = "";
            dt.Rows.InsertAt(dr, 0);
            comb1.DataSource = dt;
        }

        private void TaskView_Load(object sender, EventArgs e)
        {
            db = new DataAccess.SqlProvider(OisLibrary.DB.Connection);

            bindColumn("state", combState);
            bindColumn("class", combClass);
            dateTimePicker1.Value = DateTime.Now;

            if (tk != null)
            {
                txtTitle.Text = tk.title;

                if (tk.detail.StartsWith(@"{\rtf"))
                {
                    rtxtDetail.Rtf = tk.detail;
                }
                else
                {
                    rtxtDetail.Text = tk.detail;
                }
                combClass.Text = tk.TaskClass;
                combState.Text = tk.state;
                dateTimePicker1.Value = tk.createDate;
            }
                

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (tk == null)//新加
                {
                    tk = new Class.Task();
                    tk.hid = "0";
                }

                tk.title = ProcessSpecialLetter(txtTitle.Text);
                tk.detail = ProcessSpecialLetter(rtxtDetail.Rtf);
                tk.TaskClass = ProcessSpecialLetter(combClass.Text);
                tk.state = ProcessSpecialLetter(combState.Text);
                tk.createDate = dateTimePicker1.Value;
                string sqlText = "";
                if (tk.hid == "0")
                {
                    sqlText = string.Format(@"insert into taskList(class,title,state,date,detail) values('{0}','{1}','{2}','{3}','{4}');",
                        tk.TaskClass, tk.title, tk.state, tk.createDate.ToString("yyyy-MM-dd hh:mm:ss"), tk.detail);
                }
                else
                {
                    sqlText = string.Format(@"update taskList set class='{0}',title='{1}',state='{2}',detail='{3}' where hid='{4}'",
                        tk.TaskClass, tk.title, tk.state, tk.detail, tk.hid);
                }

                db.ExecuteSQL(sqlText);
                MessageBox.Show("保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认删除吗？","", MessageBoxButtons.OKCancel);
            if(dr==DialogResult.OK)
            if(tk!=null)
                if (tk.hid != "0")
                {
                    string sqlText = string.Format("delete tasklist where hid='{0}'", tk.hid);
                    db.ExecuteSQL(sqlText);
                }
        }

        private string ProcessSpecialLetter(string value)
        {
            value = value.Replace("'", "''");
            return value;
        }


         
    }
}
